﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;
using System.Reflection;
using System.Diagnostics;

namespace E02
{
    /// <summary>
    /// 大宗原料结算
    /// </summary>
    public class A10 : ISYNChildGet
    {
        public void Code(ISYN handler, ref CodeResult result, SYNRight user, SerialType st, String baseNo, String billId)
        {
            try
            {
                result.Code = "E02.A10";
                List<E02A10P> ht = new List<E02A10P>();
                SqlParameter[] sps = new SqlParameter[1];
                sps[0] = new SqlParameter("@FID", SqlDbType.NVarChar, 50);
                sps[0].Value = billId;
                string sql = " select a.FID BillId,a.FBillNo Number,FBillNo_SRC1 OrderNo,e.F_102 Material,c.F_102 Supplier,FComboBox BanlanceStatus,FInteger2 CancelCarNum,FDecimal12 CancelNum," +
                             "FDecimal13 BanlanceNum,FDecimal19 BanlancePrice,FDecimal20 BanlanceAmount,FDecimal14 MaterialPrice,FDecimal16 TransPrice,FDecimal18 OtherPrice," +
                             "FDecimal15 MaterialTaxRate,FDecimal17 TransTaxRate,FComboBox2 CalculateStyle,case when FDate = '' then '1990-01-01 00:00:00' when FDate IS null then '1990-01-01 00:00:00' else FDate end Date," +
                             "FNote1 Note,a.FID_SRC1 SrcID,FClassID_SRC1 SrcBillType,d.FName Checker,(select FText5 from CardServer.dbo.KEW_DataBaseEntry where FNumber = " + baseNo + " ) FiUnitID, FComboBox4 SiteArea from KEW_PurchBalanceBill a " +
                             "left join KEW_PurchItemEntry b on a.FBASE1 = b.FID left join t_ICItem e on e.FItemID = b.FBASE  " +
                             "left join t_Supplier c on a.FBASE = c.FItemID " +
                             "left join t_user d on d.FUserID = a.FUser1 and a.FUser1 <> 0 where a.FID = @FID ";
                sql = sql + " select FEntryID BillId,FBillNo_SRC BillNo,FInteger CarNum,FDecimal SendNum,FDecimal1 DeductNum,FDecimal2 BalNum," +
                            "FDecimal3 OriginalPrice,FDecimal4 DeductPrice,FDecimal5 BalPrice,FDecimal6 OriginalAmount,FDecimal7 BalAmount," +
                            "FDecimal8 TranPrice,FDecimal9 TranAmount,FDecimal10 OtherPrice,FDecimal11 OtherAmount,FComboBox1 Quality from KEW_PurchBalanceBillEntry where FID = @FID";
                sql = sql + " select FID BillId,FBillNo_SRC2 CalBillNo,FText CarNo,case when FTime = '' then '1990-01-01 00:00:00' when FTime IS null then '1990-01-01 00:00:00' else FTime end OutTime,FDecimal21 Num from KEW_PurchBalanceBillReturn where FID = @FID and FBillNo_SRC2 <> '' and FBillNo_SRC2 is not null ";
                Recordset rs = handler.Data.ExeProcedure(baseNo, sql, sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows.Count == 1)
                    {
                        DataRow row = rs.Data.Tables[0].Rows[0];
                        if (row["Material"] == null || row["Material"].ToString().Equals(""))
                        {
                            result.Result = EnumResult.None;
                            result.Message = String.Format("大宗原料结算数据提取错误:{0}", "请先配置基础资料物料对应关系！编号：" + row["Number"].ToString());
                            return;
                        }
                        if (row["Supplier"] == null || row["Supplier"].ToString().Equals(""))
                        {
                            result.Result = EnumResult.None;
                            result.Message = String.Format("大宗原料结算数据提取错误:{0}", "请先配置基础资料供应商对应关系！编号：" + row["Number"].ToString());
                            return;
                        }

                        E02A10P e02A10P = new E02A10P();
                        for (int i = 0; i < rs.Data.Tables[0].Columns.Count; i++)
                        {
                            DataColumn dc = rs.Data.Tables[0].Columns[i];
                            ReflectUtil.setValue(e02A10P, dc.ColumnName, row[dc.ColumnName].ToString());
                        }

                        List<E02A10E> OrderList = new List<E02A10E>();

                        foreach (DataRow entry in rs.Data.Tables[1].Rows)
                        {
                            E02A10E e02A10E = new E02A10E();
                            for (int j = 0; j < rs.Data.Tables[1].Columns.Count; j++)
                            {
                                DataColumn dc = rs.Data.Tables[1].Columns[j];
                                ReflectUtil.setValue(e02A10E, dc.ColumnName, entry[dc.ColumnName].ToString());
                            }
                            OrderList.Add(e02A10E);
                        }
                        e02A10P.ListBal = handler.Json.ClassToJson(OrderList);

                        List<E02A10S> CalList = new List<E02A10S>();

                        foreach (DataRow entry in rs.Data.Tables[2].Rows)
                        {
                            E02A10S e02A10S = new E02A10S();
                            for (int j = 0; j < rs.Data.Tables[2].Columns.Count; j++)
                            {
                                DataColumn dc = rs.Data.Tables[2].Columns[j];
                                ReflectUtil.setValue(e02A10S, dc.ColumnName, entry[dc.ColumnName].ToString());
                            }
                            CalList.Add(e02A10S);
                        }
                        e02A10P.ListCancel = handler.Json.ClassToJson(CalList);
                        ht.Add(e02A10P);
                        result.Content = handler.Json.ClassToJson(ht);
                    }
                    else
                    {
                        result.Result = EnumResult.None;
                    }
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = String.Format("大宗原料结算数据提取错误:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = String.Format("大宗原料结算数据提取错误:{0}", ex.Message);
            }
        }

    }
    /// <summary>
    /// 大宗原料结算
    /// </summary>
    public class E02A10P
    {
        public String BillId = String.Empty; //单据ID
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 内码
        /// </summary>
        public String ID;
        /// <summary>
        /// 结算编号
        /// </summary>
        public String Number;
        /// <summary>
        /// 订单编号
        /// </summary>
        public String OrderNo;
        /// <summary>
        /// 物料
        /// </summary>
        public String Material;
        public String Supplier = String.Empty; //供应商
        /// <summary>
        /// 结算状态
        /// </summary>
        public int BanlanceStatus = 0;
        /// <summary>
        /// 退货车数
        /// </summary>
        public int CancelCarNum = 0;
        /// <summary>
        /// 退货数量
        /// </summary>
        public Decimal CancelNum = 0;
        /// <summary>
        /// 结算数量
        /// </summary>
        public Decimal BanlanceNum = 0;
        /// <summary>
        /// 结算单价
        /// </summary>
        public Decimal BanlancePrice = 0;
        /// <summary>
        /// 结算金额
        /// </summary>
        public Decimal BanlanceAmount = 0;
        /// <summary>
        /// 物料单价
        /// </summary>
        public Decimal MaterialPrice = 0;
        /// <summary>
        /// 运输单价
        /// </summary>
        public Decimal TransPrice = 0;
        /// <summary>
        /// 其他单价
        /// </summary>
        public Decimal OtherPrice = 0;
        /// <summary>
        /// 物料税率
        /// </summary>
        public Decimal MaterialTaxRate = 0;
        /// <summary>
        /// 运输税率
        /// </summary>
        public Decimal TransTaxRate = 0;
        /// <summary>
        /// 计算方式
        /// </summary>
        public int CalculateStyle = 0;
        /// <summary>
        /// 结算列表
        /// </summary>
        public String ListBal;
        /// <summary>
        /// 退货列表
        /// </summary>
        public String ListCancel;

        public String Date;
        public String Note;

        /// <summary>
        /// 源单内码
        /// </summary>
        public String SrcID;
        /// <summary>
        /// 源单类型
        /// </summary>
        public String SrcBillType;
        /// <summary>
        /// 审核人
        /// </summary>
        public String Checker;
        /// <summary>
        /// 财务组织
        /// </summary>
        public String FiUnitID;
        /// <summary>
        ///  厂区
        /// </summary>
        public String SiteArea = String.Empty;
    }
    /// <summary>
    /// 结算列表
    /// </summary>
    public class E02A10E
    {
        public String BillId = String.Empty; //单据ID
        /// <summary>
        /// 化验单编号
        /// </summary>
        public String BillNo = String.Empty; 
        /// <summary>
        /// 车数
        /// </summary>
        public int CarNum = 0;
        /// <summary>
        /// 发货数量
        /// </summary>
        public Decimal SendNum = 0;
        /// <summary>
        /// 扣除数量
        /// </summary>
        public Decimal DeductNum = 0;
        /// <summary>
        /// 结算数量
        /// </summary>
        public Decimal BalNum = 0;
        /// <summary>
        /// 原始单价
        /// </summary>
        public Decimal OriginalPrice = 0;
        /// <summary>
        /// 扣除单价
        /// </summary>
        public Decimal DeductPrice = 0;
        /// <summary>
        /// 结算单价
        /// </summary>
        public Decimal BalPrice = 0;
        /// <summary>
        /// 物料原始金额
        /// </summary>
        public Decimal OriginalAmount = 0;
        /// <summary>
        /// 物料结算金额
        /// </summary>
        public Decimal BalAmount = 0;
        /// <summary>
        /// 运输单价
        /// </summary>
        public Decimal TranPrice = 0;
        /// <summary>
        /// 运输金额
        /// </summary>
        public Decimal TranAmount = 0;
        /// <summary>
        /// 其他单价
        /// </summary>
        public Decimal OtherPrice = 0;
        /// <summary>
        /// 其他金额
        /// </summary>
        public Decimal OtherAmount = 0;
        /// <summary>
        /// 品质
        /// </summary>
        public int Quality = 0;
    }

    /// <summary>
    /// 退货列表
    /// </summary>
    public class E02A10S
    {
        public String BillId = String.Empty; //单据ID
        /// <summary>
        /// 计量单编号
        /// </summary>
        public String CalBillNo = String.Empty;
        /// <summary>
        /// 车牌号
        /// </summary>
        public String CarNo = String.Empty;
        /// <summary>
        /// 出场时间
        /// </summary>
        public String OutTime;        
        /// <summary>
        /// 数量
        /// </summary>
        public Decimal Num = 0;
    }
}
